﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.HVAC
{
	public class HYCableTrayRoundBeam
	{
		private DialogResult IsDistanceOK(string szTilte)
		{
			return MessageBox.Show(szTilte + "值过小（过小有可能连接失败），是否采用推荐值？\n是：采用程序推荐值。\n否：采用设置值。\n取消：退出操作", "盈建科提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
		}

		private XYZ getMiddlePoint(CableTray hyODuct)
		{
			XYZ hyendPoint = (hyODuct.Location as LocationCurve).Curve.GetEndPoint(0);
			XYZ hyendPoint2 = (hyODuct.Location as LocationCurve).Curve.GetEndPoint(1);
			return new XYZ((hyendPoint.X + hyendPoint2.X) / 2.0, (hyendPoint.Y + hyendPoint2.Y) / 2.0, (hyendPoint.Z + hyendPoint2.Z) / 2.0);
		}

		private XYZ getMiddlePoint(Conduit hyODuct)
		{
			XYZ hyendPoint = (hyODuct.Location as LocationCurve).Curve.GetEndPoint(0);
			XYZ hyendPoint2 = (hyODuct.Location as LocationCurve).Curve.GetEndPoint(1);
			return new XYZ((hyendPoint.X + hyendPoint2.X) / 2.0, (hyendPoint.Y + hyendPoint2.Y) / 2.0, (hyendPoint.Z + hyendPoint2.Z) / 2.0);
		}

		public static CableTray NewPipe(CableTray sourcePipe, XYZ startXYZ, XYZ endXYZ)
		{
			CableTray cableTray = null;
			if (sourcePipe == null || startXYZ == null || endXYZ == null)
			{
				return null;
			}
			Document document = sourcePipe.Document;
			YJKDocument hydocument = new YJKDocument(document);
			foreach (ElementId elementId in ElementTransformUtils.CopyElement(document, sourcePipe.Id, new XYZ(0.0, 0.0, 0.0)))
			{
				cableTray = (hydocument.GetElement(elementId) as CableTray);
			}
			document.Regenerate();
			if (cableTray != null)
			{
				Line curve = YJKLineEx.YJKGetBound(startXYZ, endXYZ);
				(cableTray.Location as LocationCurve).Curve = curve;
			}
			document.Regenerate();
			ElementTransformUtils.MoveElement(cableTray.Document, cableTray.Id, new XYZ(0.001, 0.001, 0.001));
			ElementTransformUtils.MoveElement(cableTray.Document, cableTray.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
			return cableTray;
		}

		public static Conduit NewPipe(Conduit sourcePipe, XYZ startXYZ, XYZ endXYZ)
		{
			Conduit conduit = null;
			if (sourcePipe == null || startXYZ == null || endXYZ == null)
			{
				return null;
			}
			Document document = sourcePipe.Document;
			YJKDocument hydocument = new YJKDocument(document);
			foreach (ElementId elementId in ElementTransformUtils.CopyElement(document, sourcePipe.Id, new XYZ(0.0, 0.0, 0.0)))
			{
				conduit = (hydocument.GetElement(elementId) as Conduit);
			}
			document.Regenerate();
			if (conduit != null)
			{
				Line curve = YJKLineEx.YJKGetBound(startXYZ, endXYZ);
				(conduit.Location as LocationCurve).Curve = curve;
			}
			document.Regenerate();
			ElementTransformUtils.MoveElement(conduit.Document, conduit.Id, new XYZ(0.001, 0.001, 0.001));
			ElementTransformUtils.MoveElement(conduit.Document, conduit.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
			return conduit;
		}

		public bool NewPipePIANYI(CableTray pipe, XYZ xyz1, XYZ xyz2, double distance, double angle, string model, bool isDirect = false)
		{
			ElementId id = pipe.Id;
			bool result = false;
			Document document = pipe.Document;
			if (!isDirect)
			{
				if (model == "升降")
				{
					if (Math.Abs(distance) < 6.0 * this.getCableTrayHeight(pipe))
					{
						DialogResult dialogResult = this.IsDistanceOK("升降高度");
						if (dialogResult == DialogResult.Yes)
						{
							if (distance < 0.0)
							{
								distance = -6.0 * this.getCableTrayHeight(pipe);
							}
							else
							{
								distance = 6.0 * this.getCableTrayHeight(pipe);
							}
						}
						else if (dialogResult != DialogResult.No)
						{
							return false;
						}
					}
				}
				else if (Math.Abs(distance) < 3.2 * this.getCableTrayWidth(pipe))
				{
					DialogResult dialogResult2 = this.IsDistanceOK("偏移距离");
					if (dialogResult2 == DialogResult.Yes)
					{
						if (distance < 0.0)
						{
							distance = -3.2 * this.getCableTrayWidth(pipe);
						}
						else
						{
							distance = 3.2 * this.getCableTrayWidth(pipe);
						}
					}
					else if (dialogResult2 != DialogResult.No)
					{
						return false;
					}
				}
			}
			SubTransaction subTransaction = new SubTransaction(document);
			try
			{
				subTransaction.Start();
				HYCableTray hycableTray = new HYCableTray(pipe);
				hycableTray.CableTray.GetTypeId();
				XYZ xyz3 = hycableTray.CustomConnectorStart.Connector.CoordinateSystem.BasisX.Normalize();
				XYZ point = hycableTray.CustomConnectorStart.Connector.Origin + xyz3 * 10.0;
				if (!new HYCreatePipeHelper().judgePointIsAnotherLine(hycableTray.GetLocationCurve() as Line, point))
				{
					xyz3 = -1.0 * xyz3.Normalize();
				}
				XYZ project = hycableTray.GetProject(xyz1, true);
				XYZ project2 = hycableTray.GetProject(xyz2, true);
				IList<CableTray> source = new List<CableTray>();
				source = hycableTray.Separate(project);
				new HYCableTray(source.ElementAt(0));
				CableTray cableTray;
				CableTray piping;
				if (new HYCableTray(source.ElementAt(1)).IsPointOnCableTrayCurve(project2, 1E-06))
				{
					cableTray = source.ElementAt(0);
					piping = source.ElementAt(1);
				}
				else
				{
					cableTray = source.ElementAt(1);
					piping = source.ElementAt(0);
				}
				List<CableTray> source2 = new List<CableTray>();
				source2 = new YJKPiping<CableTray>(piping).Separate(project2);
				CableTray item;
				CableTray cableTray2;
				if (this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project) > this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project))
				{
					item = source2.ElementAt(0);
					cableTray2 = source2.ElementAt(1);
				}
				else
				{
					item = source2.ElementAt(1);
					cableTray2 = source2.ElementAt(0);
				}
				XYZ xyz4 = null;
				if (model == "偏移")
				{
					if (distance > 0.0)
					{
						xyz4 = xyz3;
					}
					else
					{
						xyz4 = -1.0 * xyz3;
					}
				}
				if (model == "升降")
				{
					if (distance > 0.0)
					{
						xyz4 = new XYZ(0.0, 0.0, 1.0);
					}
					else
					{
						xyz4 = new XYZ(0.0, 0.0, -1.0);
					}
				}
				distance = Math.Abs(distance) + cableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).AsDouble();
				ElementTransformUtils.MoveElement(document, cableTray2.Id, xyz4 * Math.Abs(distance));
				XYZ xyz5;
				XYZ xyz6;
				CableTray cableTray3;
				CableTray cableTray4;
				if (angle == Math.PI*.5)
				{
					xyz5 = project + xyz4 * Math.Abs(distance);
					xyz6 = project2 + xyz4 * Math.Abs(distance);
					cableTray3 = HYCableTrayRoundBeam.NewPipe(cableTray, project, xyz5);
					cableTray4 = HYCableTrayRoundBeam.NewPipe(cableTray, project2, xyz6);
				}
				else
				{
					xyz5 = project + xyz4 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project2 - project).Normalize();
					xyz6 = project2 + xyz4 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project - project2).Normalize();
					cableTray3 = HYCableTrayRoundBeam.NewPipe(cableTray, project, xyz5);
					cableTray4 = HYCableTrayRoundBeam.NewPipe(cableTray, project2, xyz6);
				}
				HYCableTray hycableTray2 = new HYCableTray(cableTray3);
				HYCableTray hycableTray3 = new HYCableTray(cableTray2);
				HYCableTray hycableTray4 = new HYCableTray(cableTray4);
				if (hycableTray2.IsZAxis() && hycableTray3.IsXAxis())
				{
					hycableTray2.Rotate90();
					hycableTray4.Rotate90();
				}
				else if (hycableTray2.IsZAxis() && !hycableTray3.IsYAxis())
				{
					double xaxisAngle = hycableTray3.GetXAxisAngle();
					hycableTray2.Rotate(Math.PI*.5 - xaxisAngle);
					hycableTray4.Rotate(Math.PI*.5 - xaxisAngle);
				}
				if (hycableTray2.IsZAxis())
				{
					IList<Connector> appropriateConnector = hycableTray2.GetAppropriateConnector(hycableTray3.CableTray);
					if (appropriateConnector != null && appropriateConnector.Count > 1 && appropriateConnector[0].CoordinateSystem.BasisY.IsAlmostEqualTo(appropriateConnector[1].CoordinateSystem.BasisZ))
					{
						hycableTray2.Rotate(Math.PI);
					}
					IList<Connector> appropriateConnector2 = hycableTray4.GetAppropriateConnector(hycableTray3.CableTray);
					if (appropriateConnector2 != null && appropriateConnector2.Count > 1 && appropriateConnector2[0].CoordinateSystem.BasisY.IsAlmostEqualTo(appropriateConnector2[1].CoordinateSystem.BasisZ))
					{
						hycableTray4.Rotate(Math.PI);
					}
				}
				document.Regenerate();
				HYCableTray hycableTray5 = new HYCableTray(cableTray2);
				XYZ xyz7 = (hycableTray5.EndXYZ + hycableTray5.StartXYZ) / 2.0;
				XYZ xyz8 = project + xyz4 * Math.Abs(distance);
				if (xyz5.DistanceTo(xyz7) + xyz8.DistanceTo(xyz5) > xyz8.DistanceTo(xyz7))
				{
					if (new FormPYSJWrong("shuangce").ShowDialog() == DialogResult.OK)
					{
						subTransaction.RollBack();
						document.Regenerate();
						return this.NewPipePIANYI(document.GetElementById(id) as CableTray, xyz1, xyz2, distance, Math.PI*.5, model, false);
					}
					subTransaction.RollBack();
					return false;
				}
				else
				{
					HYCableTrayConnect hycableTrayConnect = new HYCableTrayConnect();
					hycableTrayConnect.NewElbowFitting(new List<CableTray>
					{
						cableTray,
						cableTray3
					});
					this.ConduitconnetElbow(cableTray3, cableTray2, xyz5, project);
					this.ConduitconnetElbow(cableTray2, cableTray4, project2, xyz6);
					hycableTrayConnect.NewElbowFitting(new List<CableTray>
					{
						cableTray4,
						item
					});
					result = true;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(subTransaction, false);
			}
			finally
			{
				this.EndTransation(subTransaction, true);
			}
			return result;
		}

		private double getCableTrayWidth(CableTray ct)
		{
			double result;
			if (new HYCableTray(ct).CustomConnectorStart.Connector.Shape == ConnectorSharp.Round)
			{
				result = ct.Diameter;
			}
			else
			{
				result = ct.Width;
			}
			return result;
		}

		private double getCableTrayHeight(CableTray ct)
		{
			double result;
			if (new HYCableTray(ct).CustomConnectorStart.Connector.Shape == ConnectorSharp.Round)
			{
				result = ct.Diameter;
			}
			else
			{
				result = ct.Height;
			}
			return result;
		}

		private void EndTransation(SubTransaction tran, bool beSure)
		{
			if (tran != null)
			{
				TransactionStatus status = tran.GetStatus();
                if ((int)status == 1)
				{
					if (beSure)
					{
						tran.Commit();
						return;
					}
					tran.RollBack();
					return;
				}
                else if ((int)status == 5)
				{
					tran.RollBack();
				}
			}
		}

		private void ConduitconnetElbow(CableTray c1, CableTray c2, XYZ xyz1, XYZ xyz2)
		{
			HYCableTray hycableTray = new HYCableTray(c1);
			HYCableTray hycableTray2 = new HYCableTray(c2);
			Connector connector = (hycableTray.CustomConnectorStart.Connector.Origin.DistanceTo(xyz1) < hycableTray.CustomConnectorEnd.Connector.Origin.DistanceTo(xyz1)) ? hycableTray.CustomConnectorStart.Connector : hycableTray.CustomConnectorEnd.Connector;
			Connector connector2 = (hycableTray2.CustomConnectorStart.Connector.Origin.DistanceTo(xyz2) < hycableTray2.CustomConnectorEnd.Connector.Origin.DistanceTo(xyz2)) ? hycableTray2.CustomConnectorStart.Connector : hycableTray2.CustomConnectorEnd.Connector;
			c1.Document.Create.NewElbowFitting(connector, connector2);
		}

		public bool NewPipePIANYI(CableTray pipe, XYZ xyz1, double distance, double angle, string model, XYZ xyzDirection)
		{
			ElementId id = pipe.Id;
			bool result = false;
			Document document = pipe.Document;
			if (model == "升降")
			{
				if (Math.Abs(distance) < 6.0 * this.getCableTrayHeight(pipe))
				{
					DialogResult dialogResult = this.IsDistanceOK("升降高度");
					if (dialogResult == DialogResult.Yes)
					{
						if (distance < 0.0)
						{
							distance = -6.0 * this.getCableTrayHeight(pipe);
						}
						else
						{
							distance = 6.0 * this.getCableTrayHeight(pipe);
						}
					}
					else if (dialogResult != DialogResult.No)
					{
						return false;
					}
				}
			}
			else if (Math.Abs(distance) < 3.2 * this.getCableTrayWidth(pipe))
			{
				DialogResult dialogResult2 = this.IsDistanceOK("偏移距离");
				if (dialogResult2 == DialogResult.Yes)
				{
					if (distance < 0.0)
					{
						distance = -3.2 * this.getCableTrayWidth(pipe);
					}
					else
					{
						distance = 3.2 * this.getCableTrayWidth(pipe);
					}
				}
				else if (dialogResult2 != DialogResult.No)
				{
					return false;
				}
			}
			SubTransaction subTransaction = new SubTransaction(document);
			try
			{
				subTransaction.Start();
				HYCableTray hycableTray = new HYCableTray(pipe);
				XYZ xyz2 = hycableTray.CustomConnectorStart.Connector.CoordinateSystem.BasisX.Normalize();
				XYZ point = hycableTray.CustomConnectorStart.Connector.Origin + xyz2 * 10.0;
				if (!new HYCreatePipeHelper().judgePointIsAnotherLine(hycableTray.GetLocationCurve() as Line, point))
				{
					xyz2 = -1.0 * xyz2.Normalize();
				}
				XYZ project = hycableTray.GetProject(xyz1, true);
				XYZ project2 = hycableTray.GetProject(xyzDirection, true);
				IList<CableTray> source = new List<CableTray>();
				source = hycableTray.Separate(project);
				new HYCableTray(source.ElementAt(0));
				CableTray cableTray;
				CableTray cableTray2;
				if (new HYCableTray(source.ElementAt(1)).IsPointOnCableTrayCurve(project2, 1E-06))
				{
					cableTray = source.ElementAt(0);
					cableTray2 = source.ElementAt(1);
				}
				else
				{
					cableTray = source.ElementAt(1);
					cableTray2 = source.ElementAt(0);
				}
				XYZ xyz3 = null;
				if (model == "偏移")
				{
					if (distance > 0.0)
					{
						xyz3 = xyz2;
					}
					else
					{
						xyz3 = -1.0 * xyz2;
					}
				}
				if (model == "升降")
				{
					if (distance > 0.0)
					{
						xyz3 = new XYZ(0.0, 0.0, 1.0);
					}
					else
					{
						xyz3 = new XYZ(0.0, 0.0, -1.0);
					}
				}
				distance = Math.Abs(distance) + cableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).AsDouble();
				ElementTransformUtils.MoveElement(document, cableTray2.Id, xyz3 * Math.Abs(distance));
				XYZ xyz4;
				CableTray cableTray3;
				if (angle == Math.PI*.5)
				{
					xyz4 = project + xyz3 * Math.Abs(distance);
					cableTray3 = HYCableTrayRoundBeam.NewPipe(cableTray, project, xyz4);
				}
				else
				{
					xyz4 = project + xyz3 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project2 - project).Normalize();
					cableTray3 = HYCableTrayRoundBeam.NewPipe(cableTray, project, xyz4);
				}
				HYCableTray hycableTray2 = new HYCableTray(cableTray3);
				HYCableTray hycableTray3 = new HYCableTray(cableTray2);
				if (hycableTray2.IsZAxis() && hycableTray3.IsXAxis())
				{
					hycableTray2.Rotate90();
				}
				else if (hycableTray2.IsZAxis() && !hycableTray3.IsYAxis())
				{
					double xaxisAngle = hycableTray3.GetXAxisAngle();
					hycableTray2.Rotate(Math.PI*.5 - xaxisAngle);
				}
				if (hycableTray2.IsZAxis())
				{
					IList<Connector> appropriateConnector = hycableTray2.GetAppropriateConnector(hycableTray3.CableTray);
					if (appropriateConnector != null && appropriateConnector.Count > 1 && appropriateConnector[0].CoordinateSystem.BasisY.IsAlmostEqualTo(appropriateConnector[1].CoordinateSystem.BasisZ))
					{
						hycableTray2.Rotate(Math.PI);
					}
				}
				document.Regenerate();
				if (!new HYCableTray(cableTray2).IsPointOnCableTrayCurve(xyz4))
				{
					if (new FormPYSJWrong("dance").ShowDialog() == DialogResult.OK)
					{
						subTransaction.RollBack();
						document.Regenerate();
						return this.NewPipePIANYI(document.GetElementById(id) as CableTray, xyz1, distance, Math.PI*.5, model, xyzDirection);
					}
					subTransaction.RollBack();
					return false;
				}
				else
				{
					new HYCableTrayConnect().NewElbowFitting(new List<CableTray>
					{
						cableTray,
						cableTray3
					});
					this.ConduitconnetElbow(cableTray3, cableTray2, xyz4, project);
					result = true;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(subTransaction, false);
			}
			finally
			{
				this.EndTransation(subTransaction, true);
			}
			return result;
		}

		public bool NewPipePIANYI(Conduit pipe, XYZ xyz1, XYZ xyz2, double distance, double angle, string model, bool isDirect = false)
		{
			ElementId id = pipe.Id;
			bool result = false;
			Document document = pipe.Document;
			if (!isDirect)
			{
				if (model == "升降")
				{
					if (Math.Abs(distance) < 12.0 * pipe.Diameter)
					{
						DialogResult dialogResult = this.IsDistanceOK("升降高度");
						if (dialogResult == DialogResult.Yes)
						{
							if (distance < 0.0)
							{
								distance = -12.0 * pipe.Diameter;
							}
							else
							{
								distance = 12.0 * pipe.Diameter;
							}
						}
						else if (dialogResult != DialogResult.No)
						{
							return false;
						}
					}
				}
				else if (Math.Abs(distance) < 12.0 * pipe.Diameter)
				{
					DialogResult dialogResult2 = this.IsDistanceOK("偏移距离");
					if (dialogResult2 == DialogResult.Yes)
					{
						if (distance < 0.0)
						{
							distance = -12.0 * pipe.Diameter;
						}
						else
						{
							distance = 12.0 * pipe.Diameter;
						}
					}
					else if (dialogResult2 != DialogResult.No)
					{
						return false;
					}
				}
			}
			SubTransaction subTransaction = new SubTransaction(document);
			try
			{
				subTransaction.Start();
				HYConduit hyconduit = new HYConduit(pipe);
				XYZ point = new XYZ(100.0, 100.0, 100.0);
				hyconduit.GetProject(point, true);
				XYZ xyz3 = hyconduit.CustomConnectorStart.Connector.CoordinateSystem.BasisX.Normalize();
				XYZ point2 = hyconduit.CustomConnectorStart.Connector.Origin + xyz3 * 10.0;
				if (!new HYCreatePipeHelper().judgePointIsAnotherLine(hyconduit.GetLocationCurve() as Line, point2))
				{
					xyz3 = -1.0 * xyz3.Normalize();
				}
				XYZ project = hyconduit.GetProject(xyz1, true);
				XYZ project2 = hyconduit.GetProject(xyz2, true);
				IList<Conduit> source = new List<Conduit>();
				source = hyconduit.Separate(project);
				new HYConduit(source.ElementAt(0));
				Conduit conduit;
				Conduit cableTray;
				if (new HYConduit(source.ElementAt(1)).IsPointOnCableTrayCurve(project2, 1E-06))
				{
					conduit = source.ElementAt(0);
					cableTray = source.ElementAt(1);
				}
				else
				{
					conduit = source.ElementAt(1);
					cableTray = source.ElementAt(0);
				}
				IList<Conduit> source2 = new List<Conduit>();
				source2 = new HYConduit(cableTray).Separate(project2);
				Conduit c;
				Conduit conduit2;
				if (this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project) > this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project))
				{
					c = source2.ElementAt(0);
					conduit2 = source2.ElementAt(1);
				}
				else
				{
					c = source2.ElementAt(1);
					conduit2 = source2.ElementAt(0);
				}
				XYZ xyz4 = null;
				if (model == "偏移")
				{
					if (distance > 0.0)
					{
						xyz4 = xyz3;
					}
					else
					{
						xyz4 = -1.0 * xyz3;
					}
				}
				if (model == "升降")
				{
					if (distance > 0.0)
					{
						xyz4 = new XYZ(0.0, 0.0, 1.0);
					}
					else
					{
						xyz4 = new XYZ(0.0, 0.0, -1.0);
					}
				}
				distance = Math.Abs(distance) + conduit.GetParameter(BuiltInParameter.RBS_CONDUIT_DIAMETER_PARAM).AsDouble();
				ElementTransformUtils.MoveElement(document, conduit2.Id, xyz4 * Math.Abs(distance));
				XYZ xyz5;
				XYZ xyz6;
				Conduit conduit3;
				Conduit conduit4;
				if (angle == Math.PI*.5)
				{
					xyz5 = project + xyz4 * Math.Abs(distance);
					xyz6 = project2 + xyz4 * Math.Abs(distance);
					conduit3 = HYCableTrayRoundBeam.NewPipe(conduit, project, xyz5);
					conduit4 = HYCableTrayRoundBeam.NewPipe(conduit, project2, xyz6);
				}
				else
				{
					xyz5 = project + xyz4 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project2 - project).Normalize();
					xyz6 = project2 + xyz4 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project - project2).Normalize();
					conduit3 = HYCableTrayRoundBeam.NewPipe(conduit, project, xyz5);
					conduit4 = HYCableTrayRoundBeam.NewPipe(conduit, project2, xyz6);
				}
				HYConduit hyconduit2 = new HYConduit(conduit2);
				XYZ xyz7 = (hyconduit2.EndXYZ + hyconduit2.StartXYZ) / 2.0;
				XYZ xyz8 = project + xyz4 * Math.Abs(distance);
				if (xyz5.DistanceTo(xyz7) + xyz8.DistanceTo(xyz5) > xyz8.DistanceTo(xyz7))
				{
					if (new FormPYSJWrong("shuangce").ShowDialog() == DialogResult.OK)
					{
						subTransaction.RollBack();
						document.Regenerate();
						return this.NewPipePIANYI(document.GetElementById(id) as Conduit, xyz1, xyz2, distance, Math.PI*.5, model, false);
					}
					subTransaction.RollBack();
					return false;
				}
				else
				{
					this.ConduitconnetElbow(conduit, conduit3, project, project);
					this.ConduitconnetElbow(conduit3, conduit2, xyz5, project);
					this.ConduitconnetElbow(conduit2, conduit4, project2, xyz6);
					this.ConduitconnetElbow(conduit4, c, project2, project2);
					result = true;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(subTransaction, false);
			}
			finally
			{
				this.EndTransation(subTransaction, true);
			}
			return result;
		}

		private void ConduitconnetElbow(Conduit c1, Conduit c2, XYZ xyz1, XYZ xyz2)
		{
			HYConduit hyconduit = new HYConduit(c1);
			HYConduit hyconduit2 = new HYConduit(c2);
			Connector connector = (hyconduit.CustomConnectorStart.Connector.Origin.DistanceTo(xyz1) < hyconduit.CustomConnectorEnd.Connector.Origin.DistanceTo(xyz1)) ? hyconduit.CustomConnectorStart.Connector : hyconduit.CustomConnectorEnd.Connector;
			Connector connector2 = (hyconduit2.CustomConnectorStart.Connector.Origin.DistanceTo(xyz2) < hyconduit2.CustomConnectorEnd.Connector.Origin.DistanceTo(xyz2)) ? hyconduit2.CustomConnectorStart.Connector : hyconduit2.CustomConnectorEnd.Connector;
			c1.Document.Create.NewElbowFitting(connector, connector2);
		}

		public bool NewPipePIANYI(Conduit pipe, XYZ xyz1, double distance, double angle, string model, XYZ xyzDirection)
		{
			ElementId id = pipe.Id;
			bool result = false;
			Document document = pipe.Document;
			if (model == "升降")
			{
				if (Math.Abs(distance) < 12.0 * pipe.Diameter)
				{
					DialogResult dialogResult = this.IsDistanceOK("升降高度");
					if (dialogResult == DialogResult.Yes)
					{
						if (distance < 0.0)
						{
							distance = -12.0 * pipe.Diameter;
						}
						else
						{
							distance = 12.0 * pipe.Diameter;
						}
					}
					else if (dialogResult != DialogResult.No)
					{
						return false;
					}
				}
			}
			else if (Math.Abs(distance) < 12.0 * pipe.Diameter)
			{
				DialogResult dialogResult2 = this.IsDistanceOK("偏移距离");
				if (dialogResult2 == DialogResult.Yes)
				{
					if (distance < 0.0)
					{
						distance = -12.0 * pipe.Diameter;
					}
					else
					{
						distance = 12.0 * pipe.Diameter;
					}
				}
				else if (dialogResult2 != DialogResult.No)
				{
					return false;
				}
			}
			SubTransaction subTransaction = new SubTransaction(document);
			try
			{
				subTransaction.Start();
				HYConduit hyconduit = new HYConduit(pipe);
				XYZ xyz2 = hyconduit.CustomConnectorStart.Connector.CoordinateSystem.BasisX.Normalize();
				XYZ point = hyconduit.CustomConnectorStart.Connector.Origin + xyz2 * 10.0;
				if (!new HYCreatePipeHelper().judgePointIsAnotherLine(hyconduit.GetLocationCurve() as Line, point))
				{
					xyz2 = -1.0 * xyz2.Normalize();
				}
				XYZ project = hyconduit.GetProject(xyz1, true);
				XYZ project2 = hyconduit.GetProject(xyzDirection, true);
				IList<Conduit> source = new List<Conduit>();
				source = hyconduit.Separate(project);
				new HYConduit(source.ElementAt(0));
				Conduit conduit;
				Conduit conduit2;
				if (new HYConduit(source.ElementAt(1)).IsPointOnCableTrayCurve(project2, 1E-06))
				{
					conduit = source.ElementAt(0);
					conduit2 = source.ElementAt(1);
				}
				else
				{
					conduit = source.ElementAt(1);
					conduit2 = source.ElementAt(0);
				}
				XYZ xyz3 = null;
				if (model == "偏移")
				{
					if (distance > 0.0)
					{
						xyz3 = xyz2;
					}
					else
					{
						xyz3 = -1.0 * xyz2;
					}
				}
				if (model == "升降")
				{
					if (distance > 0.0)
					{
						xyz3 = new XYZ(0.0, 0.0, 1.0);
					}
					else
					{
						xyz3 = new XYZ(0.0, 0.0, -1.0);
					}
				}
				distance = Math.Abs(distance) + conduit.GetParameter(BuiltInParameter.RBS_CONDUIT_DIAMETER_PARAM).AsDouble();
				ElementTransformUtils.MoveElement(document, conduit2.Id, xyz3 * Math.Abs(distance));
				XYZ xyz4;
				Conduit conduit3;
				if (angle == Math.PI*.5)
				{
					xyz4 = project + xyz3 * Math.Abs(distance);
					conduit3 = HYCableTrayRoundBeam.NewPipe(conduit, project, xyz4);
				}
				else
				{
					xyz4 = project + xyz3 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project2 - project).Normalize();
					conduit3 = HYCableTrayRoundBeam.NewPipe(conduit, project, xyz4);
				}
				if (!new HYConduit(conduit2).IsPointOnCableTrayCurve(xyz4))
				{
					if (new FormPYSJWrong("dance").ShowDialog() == DialogResult.OK)
					{
						subTransaction.RollBack();
						document.Regenerate();
						return this.NewPipePIANYI(document.GetElementById(id) as Conduit, xyz1, distance, Math.PI*.5, model, xyzDirection);
					}
					subTransaction.RollBack();
					return false;
				}
				else
				{
					this.ConduitconnetElbow(conduit, conduit3, project, project);
					this.ConduitconnetElbow(conduit3, conduit2, xyz4, project);
					result = true;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(subTransaction, false);
			}
			finally
			{
				this.EndTransation(subTransaction, true);
			}
			return result;
		}

		public bool CableTrayRoundBeam(IList<CableTray> cableTrays, IList<XYZ> xyzs, double offSet, double angle)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (cableTrays.Count < 2 || xyzs.Count < 2)
				{
					throw new System.ArgumentNullException("cableTrays");
				}
				if (cableTrays[0].Id != cableTrays[1].Id)
				{
					throw new Exception("请选择同一个桥架");
				}
				if (0.0 == offSet)
				{
					result = true;
				}
				else
				{
					double num = Math.Tan(angle);
					if (num < 0.0001)
					{
						num = 0.0;
					}
					double num2 = 0.0;
					if (0.0 != num)
					{
						num2 = Math.Abs(offSet / num);
					}
					if (Math.Abs(num2) < 0.0001)
					{
						num2 = 0.0;
					}
					if (num2 * 2.0 >= xyzs[0].DistanceTo(xyzs[1]))
					{
						result = false;
					}
					else
					{
						Document document = cableTrays[0].Document;
						HYCableTray hycableTray = new HYCableTray(cableTrays[0]);
						ElementId elementId = cableTrays[0].get_Parameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId();
						if (!hycableTray.IsPointOnDuctCurve(hycableTray.GetProject(xyzs[0], true)) || !hycableTray.IsPointOnDuctCurve(hycableTray.GetProject(xyzs[1], true)))
						{
							throw new Exception("选择错误，升降点必须在选择的桥架上。");
						}
						if (!hycableTray.CheckFirstElbow())
						{
							throw new Exception("请设置桥架的连接首选项。");
						}
						subTransaction = new SubTransaction(document);
						subTransaction.Start();
						XYZ xyz = hycableTray.CustomConnectorStart.XYZ;
						XYZ xyz2 = hycableTray.CustomConnectorEnd.XYZ;
						ElementId typeId = hycableTray.CableTray.GetTypeId();
						XYZ globalOnCenterLinePoint = hycableTray.GetGlobalOnCenterLinePoint(xyzs[0]);
						XYZ globalOnCenterLinePoint2 = hycableTray.GetGlobalOnCenterLinePoint(xyzs[1]);
						XYZ xyz3 = (globalOnCenterLinePoint2 - globalOnCenterLinePoint).Normalize();
						XYZ xyz4 = new XYZ();
						XYZ xyz5 = new XYZ();
						xyz4 = globalOnCenterLinePoint + xyz3 * num2 + offSet * XYZ.BasisZ;
						xyz5 = globalOnCenterLinePoint2 - xyz3 * num2 + offSet * XYZ.BasisZ;
						IList<CableTray> list = hycableTray.Separate(globalOnCenterLinePoint);
						document.Regenerate();
						HYCableTray hycableTray2;
						HYCableTray hycableTray3;
						HYCableTray hycableTray4;
						if (hycableTray.IsPointOnCableTrayCurve(globalOnCenterLinePoint2))
						{
							hycableTray2 = new HYCableTray(list[1]);
							IList<CableTray> list2 = hycableTray.Separate(globalOnCenterLinePoint2);
							document.Regenerate();
							if (hycableTray.CustomConnectorStart.XYZ.IsAlmostEqualTo(xyz) || hycableTray.CustomConnectorStart.XYZ.IsAlmostEqualTo(xyz2) || hycableTray.CustomConnectorEnd.XYZ.IsAlmostEqualTo(xyz) || hycableTray.CustomConnectorEnd.XYZ.IsAlmostEqualTo(xyz2))
							{
								hycableTray3 = new HYCableTray(list2[0]);
								hycableTray4 = new HYCableTray(list2[1]);
							}
							else
							{
								hycableTray3 = new HYCableTray(list2[1]);
								hycableTray4 = new HYCableTray(list2[0]);
							}
						}
						else
						{
							HYCableTray hycableTray5 = new HYCableTray(list[1]);
							hycableTray2 = new HYCableTray(list[0]);
							IList<CableTray> list3 = hycableTray5.Separate(globalOnCenterLinePoint2);
							document.Regenerate();
							if (hycableTray5.CustomConnectorStart.XYZ.IsAlmostEqualTo(xyz) || hycableTray5.CustomConnectorStart.XYZ.IsAlmostEqualTo(xyz2) || hycableTray5.CustomConnectorEnd.XYZ.IsAlmostEqualTo(xyz) || hycableTray5.CustomConnectorEnd.XYZ.IsAlmostEqualTo(xyz2))
							{
								hycableTray3 = new HYCableTray(list3[0]);
								hycableTray4 = new HYCableTray(list3[1]);
							}
							else
							{
								hycableTray3 = new HYCableTray(list3[1]);
								hycableTray4 = new HYCableTray(list3[0]);
							}
						}
						document.Delete(hycableTray4.CableTray.Id);
						document.Regenerate();
						XYZ startXYZ = globalOnCenterLinePoint;
						XYZ endXYZ = xyz4;
						XYZ startXYZ2 = xyz5;
						XYZ endXYZ2 = globalOnCenterLinePoint2;
						CableTray cableTray = CableTray.Create(document, typeId, globalOnCenterLinePoint, xyz4, elementId);
						CableTray cableTray2 = CableTray.Create(document, typeId, xyz4, xyz5, elementId);
						CableTray cableTray3 = CableTray.Create(document, typeId, xyz5, globalOnCenterLinePoint2, elementId);
						this.ParameterConvert(hycableTray2.CableTray, cableTray, document);
						this.ParameterConvert(hycableTray2.CableTray, cableTray2, document);
						this.ParameterConvert(hycableTray2.CableTray, cableTray3, document);
						document.Regenerate();
						HYCableTray hycableTray6 = new HYCableTray(cableTray);
						hycableTray6.StartXYZ = startXYZ;
						hycableTray6.EndXYZ = endXYZ;
						HYCableTray hycableTray7 = new HYCableTray(cableTray2);
						HYCableTray hycableTray8 = new HYCableTray(cableTray3);
						hycableTray8.StartXYZ = startXYZ2;
						hycableTray8.EndXYZ = endXYZ2;
						if (hycableTray6.IsZAxis() && hycableTray7.IsXAxis())
						{
							hycableTray6.Rotate90();
							hycableTray8.Rotate90();
						}
						else if (hycableTray6.IsZAxis() && !hycableTray7.IsYAxis())
						{
							double xaxisAngle = hycableTray7.GetXAxisAngle();
							hycableTray6.Rotate(Math.PI*.5 - xaxisAngle);
							hycableTray8.Rotate(Math.PI*.5 - xaxisAngle);
						}
						if (hycableTray6.IsZAxis())
						{
							IList<Connector> appropriateConnector = hycableTray6.GetAppropriateConnector(hycableTray7.CableTray);
							if (appropriateConnector != null && appropriateConnector.Count > 1 && appropriateConnector[0].CoordinateSystem.BasisY.IsAlmostEqualTo(appropriateConnector[1].CoordinateSystem.BasisZ))
							{
								hycableTray6.Rotate(Math.PI);
							}
							IList<Connector> appropriateConnector2 = hycableTray8.GetAppropriateConnector(hycableTray7.CableTray);
							if (appropriateConnector2 != null && appropriateConnector2.Count > 1 && appropriateConnector2[0].CoordinateSystem.BasisY.IsAlmostEqualTo(appropriateConnector2[1].CoordinateSystem.BasisZ))
							{
								hycableTray8.Rotate(Math.PI);
							}
						}
						document.Regenerate();
						HYCableTrayConnect hycableTrayConnect = new HYCableTrayConnect();
						hycableTrayConnect.NewElbowFitting(new List<CableTray>
						{
							hycableTray2.CableTray,
							cableTray
						});
						hycableTrayConnect.NewElbowFitting(new List<CableTray>
						{
							cableTray,
							cableTray2
						});
						hycableTrayConnect.NewElbowFitting(new List<CableTray>
						{
							cableTray2,
							cableTray3
						});
						hycableTrayConnect.NewElbowFitting(new List<CableTray>
						{
							cableTray3,
							hycableTray3.CableTray
						});
						document.Regenerate();
						subTransaction.Commit();
						result = true;
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private void ParameterConvert(CableTray sourceCableTray, CableTray newCableTray, Document document)
		{
			SubTransaction subTransaction = new SubTransaction(document);
			subTransaction.Start();
			foreach (object obj in newCableTray.Parameters)
			{
				Parameter parameter = (Parameter)obj;
                switch ((int)parameter.StorageType)
				{
				case 1:
					if (!parameter.IsReadOnly)
					{
						parameter.Set(sourceCableTray.get_Parameter(parameter.Definition).AsInteger());
					}
					break;
				case 2:
					if (parameter.Id.IntegerValue != -1114132 && !parameter.IsReadOnly)
					{
						parameter.Set(sourceCableTray.get_Parameter(parameter.Definition).AsDouble());
					}
					break;
				case 3:
					if (!parameter.IsReadOnly && parameter.IsShared)
					{
						parameter.Set(sourceCableTray.get_Parameter(parameter.Definition).AsString());
					}
					break;
				case 4:
					parameter.AsElementId();
					if (!parameter.IsReadOnly)
					{
						parameter.Set(sourceCableTray.get_Parameter(parameter.Definition).AsElementId());
					}
					break;
				}
			}
			if ((int)subTransaction.Commit() == 5)
			{
				subTransaction.RollBack();
			}
		}
	}
}
